package com.example.demo.singletonMode;

import java.lang.reflect.Constructor;

/**
 * 枚举类可阻止反射破坏单例
 */
public enum Sign {

    INSTANCE;

    public static Sign getInstance(){
        return INSTANCE;
    }

}

class test{

    public static void main(String[] args) throws Exception{
        Sign instance = Sign.INSTANCE;
        //获取类的无参构造发现错误，没有此构造方法，通过jad工具反编译发现，该类继承的有枚举类，是有参构造
//        Constructor<Sign> declaredConstructor = Sign.class.getDeclaredConstructor(null);
        //通过jad工具反编译发现，该类继承的有枚举类，是有参构造，通过获取此有参构造，就能看到正确结果
        Constructor<Sign> declaredConstructor = Sign.class.getDeclaredConstructor(String.class,int.class);
        declaredConstructor.setAccessible(true);
        Sign instance2 = declaredConstructor.newInstance();
        System.err.println(instance);
        System.err.println(instance2);
    }

}
